package com.bq.gametrade.ui.dialog;

import android.Manifest;
import android.app.Activity;
import android.app.DialogFragment;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.bq.gametrade.R;
import com.bq.gametrade.ui.activity.PermissionsActivity;
import com.bq.gametrade.utils.PermissionsChecker;
import com.bq.gametrade.view.interceptionicon.CropHandler;
import com.bq.gametrade.view.interceptionicon.CropHelper;
import com.bq.gametrade.view.interceptionicon.CropParams;

/**
 * Created by Fei on 2016/1/13.
 */
public class CuttingPictureDialog extends DialogFragment implements View.OnClickListener, CropHandler {
    public static final String TAG = "InterceptionImageDialog";
    private CropParams mCropParams;
    private TextView tv_taking_pictures, tv_photo_album, tv_cannel;
    private Activity activity;

    public interface HeadIconPathListener {
        void OnHeadIconPath(String path);
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activity = getActivity();
        mCropParams = new CropParams(activity);
        mPermissionsChecker = new PermissionsChecker(getActivity());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = inflater.inflate(R.layout.dialog_cuttingpicture, container);
        setStyle(DialogFragment.STYLE_NORMAL, R.style.shareDialog_style);
        Window window = getDialog().getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.setBackgroundDrawable(new ColorDrawable(0xffffffff));
        WindowManager.LayoutParams lp = getDialog().getWindow().getAttributes();
        lp.alpha = 1.0f; // 透明度
        window.setAttributes(lp);
        getDialog().show();
        initView(view);
        return view;
    }

    private void initView(View view) {
        tv_taking_pictures = (TextView) view.findViewById(R.id.tv_taking_pictures);
        tv_taking_pictures.setOnClickListener(this);
        tv_photo_album = (TextView) view.findViewById(R.id.tv_photo_album);
        tv_photo_album.setOnClickListener(this);
        tv_cannel = (TextView) view.findViewById(R.id.tv_cannel);
        tv_cannel.setOnClickListener(this);

    }

    // 所需的全部权限
    static final String[] PERMISSIONS = new String[]{
            Manifest.permission.CAMERA
    };
    private PermissionsChecker mPermissionsChecker; // 权限检测器

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.tv_taking_pictures: {
                mCropParams.enable = true;
                mCropParams.compress = false;
                Intent intent = CropHelper.buildCameraIntent(mCropParams);
                try {
                    startActivityForResult(intent, CropHelper.REQUEST_CAMERA);
                } catch (Exception e) {
                    if (mPermissionsChecker.lacksPermissions(PERMISSIONS)) {
                        startPermissionsActivity();
                    }
                    return;
                }

            }
            break;
            case R.id.tv_photo_album: {
                mCropParams.enable = true;
                mCropParams.compress = false;
                Intent intent = CropHelper.buildGalleryIntent(mCropParams);
                startActivityForResult(intent, CropHelper.REQUEST_CROP);

            }
            break;
            case R.id.tv_cannel:
                getDialog().dismiss();
                break;
        }

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1) {
        }

        // 拒绝时, 关闭页面, 缺少主要权限, 无法运行
        if (requestCode == REQUEST_CODE && resultCode == PermissionsActivity.PERMISSIONS_DENIED) {

            dismiss();
        } else if (requestCode == REQUEST_CODE && resultCode == PermissionsActivity.PERMISSIONS_GRANTED) {
            mCropParams.enable = true;
            mCropParams.compress = false;
            Intent intent = CropHelper.buildCameraIntent(mCropParams);
            startActivityForResult(intent, CropHelper.REQUEST_CAMERA);
        } else {
            CropHelper.handleResult(this, requestCode, resultCode, data);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public CropParams getCropParams() {
        return mCropParams;
    }

    @Override
    public void onPhotoCropped(Uri uri) {
        // Original or Cropped uri
        if (!mCropParams.compress) {
            HeadIconPathListener listener = (HeadIconPathListener) getActivity();
            listener.OnHeadIconPath(uri.getPath());
            getDialog().dismiss();
        }
//            mImageView.setImageBitmap(BitmapUtil.decodeUriAsBitmap(this, uri));
    }

    @Override
    public void onCompressed(Uri uri) {
        getDialog().dismiss();

        // Compressed uri
//        mImageView.setImageBitmap(BitmapUtil.decodeUriAsBitmap(this, uri));
    }

    @Override
    public void onCancel() {

        Toast.makeText(getActivity(), "取消拍照", Toast.LENGTH_LONG).show();
        getDialog().dismiss();

    }

    @Override
    public void onFailed(String message) {
        Toast.makeText(getActivity(), "拍照失败: " + message, Toast.LENGTH_LONG).show();
        getDialog().dismiss();

    }

    @Override
    public void handleIntent(Intent intent, int requestCode) {
        startActivityForResult(intent, requestCode);
    }

    private static final int REQUEST_CODE = 0; // 请求码

    private void startPermissionsActivity() {
        PermissionsActivity.startActivityForResult(getActivity(), REQUEST_CODE, PERMISSIONS);
    }
}
